package oyy.servlet;

import net.sf.json.JSONObject;
import oyy.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/getuid")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        int id = Integer.parseInt(uid);
        User user = null;
        switch(id){
            case 111:
                user = new User(111,"冰墩墩","北京","用户名已重复");
                break;
            case 222:
                user = new User(222,"东方明珠","上海","用户名已重复");
                break;
            case 333:
                user = new User(333,"羊城","广州","用户名已重复");
                break;
            default:
                user = new User(0,"","","用户名可用");
        }
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        // java->json
        JSONObject jsonObject = JSONObject.fromObject(user);
        writer.print(jsonObject);
        //System.out.println(jsonObject);
    }
}
